﻿using BLL;
using Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace echuCMS
{
    public partial class FormCustomerApproach : Form
    {
        private BindingSource bsCustomerMain = new BindingSource();
        private BindingSource bsCustomerContract = new BindingSource();
        private BindingSource bsCustomerFinance = new BindingSource();
        private BindingSource bsCustomerDeliver = new BindingSource();
        public FormCustomerApproach()
        {
            InitializeComponent();
        }

        private void FormCustomerApproach_Load(object sender, EventArgs e)
        {
            dataGridViewCustomerMain.AutoGenerateColumns = false;
            dataGridViewDeliver.AutoGenerateColumns = false;
            dataGridViewFinance.AutoGenerateColumns = false;
            dataGridViewContract.AutoGenerateColumns = false;
            //展示所有不在 潜在 公共 黑名单 的客户
            List<CustomerMain> customerMainList = CustomerMainService.getNoApproachCustomer();
            bsCustomerMain.DataSource = customerMainList;
            dataGridViewCustomerMain.DataSource = bsCustomerMain;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridViewCustomerMain.Rows.Count; i++)
            {
                if ((Convert.ToBoolean(dataGridViewCustomerMain.Rows[i].Cells["F选择"].Value) == false))
                {
                    dataGridViewCustomerMain.Rows[i].Cells["F选择"].Value = "True";
                }
                else
                {
                    continue;
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dataGridViewCustomerMain.Rows.Count; i++)
            {
                if ((Convert.ToBoolean(dataGridViewCustomerMain.Rows[i].Cells["F选择"].Value) == true))
                {
                    dataGridViewCustomerMain.Rows[i].Cells["F选择"].Value = "False";
                }
                else
                {
                    continue;

                }
            }
        }

        private void dataGridViewCustomerMain_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > 0)
            {
                CustomerMain customerMain = bsCustomerMain.Current as CustomerMain;
                //显示详细信息
                bsCustomerContract.DataSource = customerMain.customerContractList;
                dataGridViewContract.DataSource = bsCustomerContract;
                bsCustomerDeliver.DataSource = customerMain.customerDeliverList;
                dataGridViewDeliver.DataSource = bsCustomerDeliver;
                bsCustomerFinance.DataSource = customerMain.customerFinanceList;
                dataGridViewFinance.DataSource = bsCustomerFinance;
            }
        }

        private void buttonApproach_Click(object sender, EventArgs e)
        {
           // List<CustomerMain> customerMainList = new List<CustomerMain>();
            List<CustomerPublic> customerPublicList = new List<CustomerPublic>();
            for (int i = 0; i < dataGridViewCustomerMain.Rows.Count; i++)
            {
                if (dataGridViewCustomerMain.Rows[i].Cells["F选择"].Value != null)
                {
                    if ((Convert.ToBoolean(dataGridViewCustomerMain.Rows[i].Cells["F选择"].Value) == true))
                    {
                        
                        CustomerMain customerMain = bsCustomerMain[i] as CustomerMain;
                        CustomerPublic customerPublic = new CustomerPublic();
                        customerPublic.CustomerMainId = customerMain.Id;
                        customerPublic.Status = 1;
                        
                        customerPublicList.Add(customerPublic);
                    }
                }
            }
            //将客户放入潜在客户
            int r = CustomerMainService.customerToPublic(customerPublicList);
            if (r > 0)
            {
                MessageBox.Show("审核成功");
            }
            else
            {
                MessageBox.Show("审核失败，请联系开发者");
            }
        }
    }
}
